package com.ylyq.lingoserver.service.impl;

import com.ylyq.lingoserver.constants.AccountConstant;
import com.ylyq.lingoserver.lingo.LingoInvoker;
import com.ylyq.lingoserver.model.ResponseData;
import com.ylyq.lingoserver.model.ResultCore;
import com.ylyq.lingoserver.service.ILingoService;
import com.ylyq.lingoserver.util.BufferedWriterUtil;
import com.ylyq.lingoserver.util.Tools;
import lombok.extern.slf4j.Slf4j;
import org.ini4j.Wini;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @ClassName LingoServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/15 16:41
 * @Version 1.0
 */
@Slf4j
@Service
public class LingoServiceImpl implements ILingoService {

    @Value("${public.config.lingo.lingoPath}")
    private String lingoPath;
    @Value("${public.config.lingo.inputSuffix}")
    private String inputSuffix;
    @Value("${public.config.lingo.outputSuffix}")
    private String outputSuffix;
    @Value("${public.config.lingo.lingoSuffix}")
    private String lingoSuffix;

    @Override
    public ResponseData excuteLingo(Map message) throws IOException {

//        List<YlyqCoreTag> ylyqCoreTags = (List<YlyqCoreTag>) message.get("resultCore");
        String resultCore = (String) message.get("resultCore");


        LingoInvoker invoker = new LingoInvoker();

        String uuid = Tools.getUUID();

        String inputPath = lingoPath + uuid + inputSuffix;
        String outputPath = lingoPath +  uuid + outputSuffix;


        Map<String, String> map = new HashMap<>();
        map.put("${input}", inputPath);
        map.put("${output}", outputPath);
        String newLingoFilePath = lingoPath + uuid + lingoSuffix;

        File outputFile = new File(outputPath);
        if (!outputFile.exists()) {
            outputFile.createNewFile();
        }

        File inputFile = new File(inputPath);
//        StringBuilder parms = new StringBuilder();
//        parms.append(ylyqCoreTags.size()+"~\n");
//        for (YlyqCoreTag temp:ylyqCoreTags) {
//            parms.append(temp.getDx()+" "+temp.getZpl()+" "+temp.getXl()+" "+temp.getKk()+" "+temp.getCz()+" "+temp.getBhy()+" "+temp.getX()+" "+temp.getMs()+" "+temp.getSs()+" "
//                    +temp.getXiaoLv()+" "+temp.getBd()+" "+temp.getRt()+" "+temp.getPb()+" "+temp.getHq()+" "+temp.getHeQun()+" "+temp.getHqxx()+" "+temp.getYq()+"\n");
//        }
//        parms.append(ylyqCoreTags.size()+"~\n");
//        parms.append("72 55 9 5 52 33 49 91 96 29 50 96 42 4 80 37 5~");


//        parms.append("2~\n" +
//                "2 4 2 1 0 0 0 0 1 0 2 0 0 0 4 4 2\n" +
//                "0 4 0 1 2 2 2 4 2 0 0 2 2 1 2 3 0~\n" +
//                "72 55 9 5 52 33 49 91 96 29 50 96 42 4 80 37 5~");
//        int i = BufferedWriterUtil.writerIntoFile(inputFile, parms.toString());
        int i = BufferedWriterUtil.writerIntoFile(inputFile, resultCore);
        if (i == AccountConstant.FAIL) {
            log.error(inputFile.getAbsolutePath() + "输入文件生成失败");
            return new ResponseData(ResponseData.Result.FAIL.getCode(), inputFile.getAbsolutePath() + "输入文件生成失败");
        }
        String path = ResourceUtils.getURL("classpath:").getPath().replace("/target/classes", "");
        Integer integer = BufferedWriterUtil.replaceStringFromFileToFile(path + "\\src\\main\\resources\\lingo\\lingosh.lng", newLingoFilePath, map);
        //因为是moudle的方式，所以下面的方式获取的path不是该moudle的目录
//        D:\project\ylyq-parent\src\main\resources\lingo\lingosh.lng
//        Integer integer = BufferedWriterUtil.replaceStringFromFileToFile(System.getProperty("user.dir") + "\\lingo-server\\src\\main\\resources\\lingo\\lingosh.lng", newLingoFilePath, map);
        if (integer == AccountConstant.FAIL) {
            log.error(newLingoFilePath + "lingo文件生成失败");
            return new ResponseData(ResponseData.Result.FAIL.getCode(), newLingoFilePath + "lingo文件生成失败");
        }

//        if (invoker.solve(newLingoFilePath) && invoker.getErrorCode() == -1) {
//            log.info("***DONE***");
//        } else {
//            log.info("未结束呢");
//        }

        if (invoker.solve(newLingoFilePath)) {
            File resultFile = new File(outputPath);

            if (!resultFile.exists()) {
                log.error("lingo ={}执行结束 输出文件不存在", outputPath);
                return null;
            }

            //InputStreamReader isr = new InputStreamReader(new FileInputStream(resultFile),"GBK");
//取结果
            Wini ini = new Wini(resultFile);
            String jdm = "状态";
            String solveStatus = ini.get(jdm, "solve_status");

            String jdm2 = "结果";


//            for (int j = 0; j < size; j++) {
//
//            }

            int mealNums = Integer.parseInt(ini.get(jdm2, "人群编号-1")); // 参与运算的 套餐数
            int cardNums = Integer.parseInt(ini.get(jdm2, "人群编号-2")); // 参与运算的 流量卡数

            log.info(jdm+solveStatus);
            log.info(jdm2+mealNums+cardNums);

        }

        return new ResponseData(ResponseData.Result.SUCCESS);
    }

}
